package JAVAcollectionsanddatastructures.enumdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

//既然枚举的构造方法是私有的，那么是否可以通过反射来获取枚举对象的实例呢？
//答：枚举非常安全，通过反射都拿不到枚举的实例对象
public class TestEnumReflect {
    public static void reflectPrivateConstructor() {
        try {
            Class<?> classEnum = Class.forName("JAVAcollectionsanddatastructures.enumdemo.TestEnum");

            Constructor<?> dl = classEnum.getDeclaredConstructor(String.class, int.class, String.class, int.class);
            dl.setAccessible(true);

            Object objectStudent = ((Constructor<?>) dl).newInstance("green", 666, "tc", 99);
            TestEnum testEnum = (TestEnum) objectStudent;
            System.out.println("获得枚举的私有构造函数：" + testEnum);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        reflectPrivateConstructor();
    }



}

